package com.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * 
 *     
 * 项目名称：潜隆贷后台管理   
 * 类名称：AnalysisXml      
 * 创建人：summer    
 * 创建时间：2014-12-18 下午12:06:20    
 * 修改人：summer   
 * 修改时间：2014-12-18 下午12:06:20    
 * 修改备注：    
 * @version     
 *
 */
public class AnalysisXml {

    public static Map<String, XMLBean> getStrustsXMLBean(String servletpath) throws JDOMException {

        Map<String, XMLBean> map = new HashMap<String, XMLBean>();

        XMLBean beans = null;

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(new File(servletpath));

        Element root = doc.getRootElement();

        Element formMappings = root.getChild("action-mappings");

        List<Element> mappings = formMappings.getChildren();

        for (Element m : mappings) {

            beans = new XMLBean();

            String formName = m.getAttributeValue("name");

            beans.setFormName(formName);// 设置fromName

            Element formBeans = root.getChild("form-beans");

            List<Element> be = formBeans.getChildren();

            for (Element b : be) {

                String beanNames = b.getAttributeValue("name");

                if (formName.equals(beanNames)) {

                    String formClass = b.getAttributeValue("type");

                    beans.setFromClass(formClass);// 设置formClass

                    break;
                }

            }
            
            List<Element> forward=m.getChildren();//获取 action下面的forward
            
            Map<String,String> forwardMap=new HashMap<String,String>();
            for (Element e : forward) {
                
                String forwardName=e.getAttributeValue("name");
                
                String forwardPath=e.getAttributeValue("path");
                
                forwardMap.put(forwardName, forwardPath);
                
                beans.setMap(forwardMap);
                
            }
            

            String acitonPath = m.getAttributeValue("path");

            beans.setAcitonPath(acitonPath);// 设置actionPath

            String actionClass = m.getAttributeValue("type");

            beans.setActionClass(actionClass);// 设置acitonClass

        
            
            beans.setAcitonName(formName);// 设置acitonName=formName

            
            map.put(acitonPath, beans);
            

        }

        return map;

    }

    public static void main(String [] args) {

    }

}
